/*
 * Copyright (c) 2015. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.tsinghuabigdata.edu.ddmath.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.util.AlertManager;

import org.apache.http.conn.ConnectTimeoutException;

/**
 * <p>
 * Created by yanshen@tsinghuabigdata.com on 2015/12/17.
 * </p>
 * <p/>
 * 网络加载故障提示界面
 *
 * @author yanshen@tsinghuabigdata.com
 * @version V1.0
 * @packageName: com.tsinghuabigdata.edu.ddmath.controls
 * @createTime: 2015/12/17 14:13
 */
public class ProgressAnimationView extends LoadDataFaultView {

    private View loadingView;
    private ImageView gifview;

    private AnimationDrawable animationDrawable;

    public ProgressAnimationView(Context context) {
        super(context);
        initialize();
    }

    public ProgressAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public ProgressAnimationView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    /**
     * 初始化界面元素
     */
    private void initialize() {
        loadingView = LayoutInflater.from(getContext()).inflate(R.layout.layout_loading, this, false);
        gifview = (ImageView) loadingView.findViewById(R.id.giv_loading);

        addView(loadingView, getChildCount());

        animationDrawable = (AnimationDrawable) gifview.getDrawable();
        startAnim();
    }

    public void setFaultView( Exception ex ){
        loadingView.setVisibility(GONE);
        exception_cause = AlertManager.getErrorInfo( ex );
    }

    @Override
    public void show() {
        loadingView.setVisibility(VISIBLE);
        loadingView.setBackgroundColor(Color.WHITE);
        super.show();
    }

    public void showProgress(){
        loadingView.setVisibility(VISIBLE);
    }
    public void hideProgress(){
        loadingView.setVisibility(GONE);
    }
    public void showFaultView(){
        hideProgress();
        loadingView.setVisibility(GONE);
        super.show();
    }

    private void startAnim() {
        animationDrawable.start();
    }
}
